<?php 
define('CONFIG',__DIR__);
// 设置定时发送时间间隔
$time = '80';

require CONFIG. '/capture.php';
require CONFIG ."/function.php";
//文章中的最后一段图片的路径
$img_gg = require(CONFIG.'/img.php');
$img_gg = $img_gg[array_rand($img_gg)];
print_R("任务开始\n");
$curl = new Curl();

$user = require(CONFIG.'/config.php');
	
$jsonArr = csv2json(dirname(__FILE__) . '/hosTag.csv', $csvTabName);
$hosRepKey = array();
foreach ($jsonArr as $value) {
    // 分解医生信息
    $value['DoctorName'] = explode('/', $value['DoctorName']);
    // 分解手机站地址
    $value['mobileSite'] = explode('/', $value['mobileSite']);
    $hosRepKey[$value['hosName']] = $value;
}

//循环医院
foreach($user as $key => $val){
	//随机生成需要插入的图片
	$img = require(CONFIG.'/img/'.$key.'.php');
	//当前医院
	print_R('当前医院:'.$key."\n");

	//引入对应文章的首标题
	$title = require(CONFIG.'/title.php');
	    
	//循环用户
	foreach($val['user'] as $k => $v){
		//当前用户信息
			$user_info = $v;
		//引入对应键的对应医院名
		$txt_url = CONFIG.'/yy/'.$user_info['userName'].'.txt';
		$txt_content = file_get_contents($txt_url);

		$str_encoding = mb_convert_encoding($txt_content, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//转换字符集（编码）
	    $arr = explode("\r\n", $str_encoding);//转换成数组
	    //去除值中的空格
	    foreach ($arr as &$row) {
	        $row = trim($row);
	    }
	    unset($row);
		
		//进入登录页面
			$curl->header(array(

			    'Referer:https://www.wang1314.com/login/',

			    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',

			))->get('https://www.wang1314.com/login/');

			$curl->cookie($curl->Cookies)->post('https://www.wang1314.com/login.do', array(

		    //获取用户信息
		    'userId' => $user_info['userName'],

		    'psw' => $user_info['userPass'],

		    'Submit' => '登录网络收藏夹',

		    'fromUri' => 'https://www.wang1314.com/doc/PrepareForNewTopicAction.do'

			));
			$curl->cookie($curl->Cookies)->get('https://www.wang1314.com/doc/my/');
		//当前用户
		print_R("当前用户:".$v['userName']."\n");
		//每个用户发送40次
		for ($i=0; $i < 40 ; $i++) { 
			//获取文件的保存值
			$res = getTitleId(CONFIG."/yy/{$user_info['userName']}.txt")?getTitleId(CONFIG."/yy/{$user_info['userName']}.txt"):0;
		
			$img_num = $img[array_rand($img)];
			
			//获取不定时的需要插入的用户信息
			$mark = $user_info['mark'];

			//获取发送文章类型内容
			$article = "http://dal.120open.com/Home/Api/article/?mark=$mark&num=1";

			$content = file_get_contents($article);

			$content = str_ireplace(['jsonpReturn(', ');'], '', $content);

			$article = json_decode($content, true);
			//获取标题
			$arr_title = $arr[$res];

			//获取对应医院的随机标题
			$title_info = $title[$key][array_rand($title[$key])];

			//拼接图片传入需要插入的文章内容
			$article_content = addimg($article[0]['body'],$key,$img_num,trim($img_gg),$title_info);
			//文章标签
		
			$topic_tags = $val['tag'][array_rand($val['tag'])];

			$article = postBodyReplace($article_content,$user_info['hosType'],$arr_title);

			$html = $curl->cookie($curl->Cookies)->get('https://www.wang1314.com/doc/PrepareForNewTopicAction.do');
			//正则获取随机更改的$randomId字段
			preg_match('#name="randomId" value="(.*?)"#', $html, $randomId);
			//获取需要提交的上传内容
			$data  = array(
		        'topic_mode'         => '0',
		        'topic_title'        => $arr_title,//标题
		        'post_expression'    => 'face1.gif',
		        'topic_body'         => $article,//内容
		        'topic_tags'         => $topic_tags,
		        'topic_publicStates' => '0',
		        'isAjax'             => '1',
		        'randomId'           => $randomId[1],
		        'topic_from'         => ''
	    	);	
	    	print_R($arr_title."\n");
	    	//给文件保存值加1
	    	setTitleId(CONFIG."/yy/{$user_info['userName']}.txt",$unm = $res == count($arr)-1?0:$res+1);

			$res = $curl->cookie($curl->Cookies)->post('https://www.wang1314.com/doc/AddNewTopicAction.do',$data);

			sleep($time);
		}
	}
}

print_R("任务完毕\n");

require CONFIG.'/swoop.php';





























 ?>